<template>
    <div>
        <div><h1>购买商品</h1></div>
        <div style="margin-bottom: 20px;">
            <el-descriptions
                class="margin-top"
                title="商品信息"
                :column="3"
                :size="size"
                border
            >
                <template #extra>
                    
                </template>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            商品名称
                        </div>
                    </template>
                    {{prod.prodName}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            商品类型
                        </div>
                    </template>
                    {{prod.typeName}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            品牌
                        </div>
                    </template>
                    {{prod.brandName}}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            单位
                        </div>
                    </template>
                    <el-tag size="small">{{prod.unitName}}</el-tag>
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            供应商
                        </div>
                    </template>
                    {{ prod.apprName }}
                </el-descriptions-item>
                <el-descriptions-item>
                    <template #label>
                        <div class="cell-item">
                            图片
                        </div>
                    </template>
                    <el-image :src="prod.prodImage" style="width: 120px;height: 120px;"></el-image>
                </el-descriptions-item>
            </el-descriptions>
        </div>

        <div>
            <el-button type="primary" @click="onSubmitBuy">马上购买</el-button>
        </div>
        <div>
            <span>您的的订单金额是：{{ buyAmount }} 元。</span>
        </div>

        <div>
            <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="infoId" label="ID" width="180" />
                <el-table-column prop="prodSku" label="SKU" width="180" />
                <el-table-column prop="prodName" label="商品" width="180" />
                <el-table-column prop="prodStock" label="库存" width="180" />
                <el-table-column
                    prop="alertStock"
                    label="预警库存"
                    width="180"
                />
                <el-table-column prop="prodPrice" label="售价" width="180" />

                <el-table-column label="购买数量" width="180">
                    <template #default="scope">
                        <el-input-number v-model="scope.row.buyNum" :min="0" :max="scope.row.prodStock" @change="handleBuyChange" />
                    </template>
                </el-table-column>

                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getProdInfoList,addOrder } from "@/api/api";
import { computed } from 'vue'
import {
  Iphone,
  Location,
  OfficeBuilding,
  Tickets,
  User,
} from '@element-plus/icons-vue'
import { ElMessage, type ComponentSize } from 'element-plus'

const prod = ref({});
const tableData = ref([]);
const total = ref(0);
const buyAmount = ref(0);

const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    typeId: null,
});

const size = ref<ComponentSize>('default')

const multipleSelection = ref<any[]>([])

// 购买
const onSubmitBuy = () => {
    console.log('购买');
    // 判断是否选择商品
    if(multipleSelection.value.length == 0) {
        ElMessage.error('请选择商品');
        return;
    }

    // 下单
    addOrder(multipleSelection.value).then(res => {
        if(res.data.code == 200) {
            ElMessage.success('下单成功');
            loadProdInfoList();
        }
        else {
             ElMessage.error(res.data.msg);
        }
    })
}

const handleBuyChange = () => {
    console.log('购买数量改变');
    // 计算订单金额
    buyAmount.value = 0;
        multipleSelection.value.forEach((item) => {
        buyAmount.value += item.prodPrice * item.buyNum;
    });
}

const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log('要购买的商品信息', multipleSelection.value);

  // 计算订单金额
  buyAmount.value = 0;
  multipleSelection.value.forEach((item) => {
    buyAmount.value += item.prodPrice * item.buyNum;
  });
}

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searchForm.value.pageSize = val;
    loadProdInfoList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searchForm.value.pageNum = val;
    loadProdInfoList();
};

//
const loadProdInfoList = () => {
    getProdInfoList(searchForm.value).then((res) => {
        if (res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    });
};

// 页面钩子函数
onMounted(() => {
    let s = sessionStorage.getItem("buy");
    prod.value = JSON.parse(s);
    searchForm.value.typeId = prod.value.prodId;
    console.log("prodInfo", prod.value);

    // 加载列表
    loadProdInfoList();
});
</script>

<style scoped></style>
